package com.jeesite.modules.stock.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeesite.modules.stock.dao.StockFinaceDao;
import com.jeesite.modules.stock.dto.vo.ChartLineDataVO;

@Service
public class StockFinaceChartService {

    @Autowired
    StockFinaceDao stockFinaceDao;

    private Double getDoubleData(Map<String, Object> data, String key) {
        if (data.containsKey(key)) {
            Object value = data.get(key);
            if (value instanceof String sv) {
                return Double.parseDouble(sv);
            }
            if (value instanceof Double) {
                return (Double) value;
            }
            if (value instanceof BigDecimal bv) {
                return bv.doubleValue();
            }
        }
        return 0d;
    }

    private int getSeason(String date) {
        int season = -1;
        if (date != null && date.length() == 10) {
            int month = Integer.parseInt(date.split("-")[1]);
            if (month == 12) {
                season = 4;
            } else if (month == 9) {
                season = 3;
            } else if (month == 6) {
                season = 2;
            } else {
                season = 1;
            }
        }
        return season;
    }


    /**
     * 获取股票利润率折线数据
     *
     * @param stockCode 股票代码
     */
    public List<ChartLineDataVO> queryNetProfitList(String stockCode) {
        Map<String, Object> data = stockFinaceDao.getStockLatestData(stockCode);
        String yearFirst = String.valueOf(data.get("report_period_first")).split("-")[0];
        String yearSecond = String.valueOf(data.get("report_period_second")).split("-")[0];
        String yearThird = String.valueOf(data.get("report_period_third")).split("-")[0];
        List<ChartLineDataVO> vos = new ArrayList<>();
        int s1 = getSeason(String.valueOf(data.get("report_period_first")));
        int s2 = getSeason(String.valueOf(data.get("report_period_second")));
        int s3 = getSeason(String.valueOf(data.get("report_period_third")));
        vos.add(new ChartLineDataVO("环比增长", yearThird + "Q" + s3, getDoubleData(data, "net_profit_qoq_growth_rate_third")));
        vos.add(new ChartLineDataVO("环比增长", yearSecond + "Q" + s2, getDoubleData(data, "net_profit_qoq_growth_rate_second")));
        vos.add(new ChartLineDataVO("环比增长", yearFirst + "Q" + s1, getDoubleData(data, "net_profit_qoq_growth_rate_first")));

        vos.add(new ChartLineDataVO("同比增长", yearThird + "Q" + s3, getDoubleData(data, "net_profit_yoy_growth_rate_third")));
        vos.add(new ChartLineDataVO("同比增长", yearSecond + "Q" + s2, getDoubleData(data, "net_profit_yoy_growth_rate_second")));
        vos.add(new ChartLineDataVO("同比增长", yearFirst + "Q" + s1, getDoubleData(data, "net_profit_yoy_growth_rate_first")));
        return vos;
    }

    /**
     * 获取股票营收增长率折线图数据(同比和环比)
     *
     * @param stockCode 股票代码
     */
    public List<ChartLineDataVO> queryRenevueList(String stockCode) {
        Map<String, Object> data = stockFinaceDao.getStockLatestData(stockCode);
        String yearFirst = String.valueOf(data.get("report_period_first")).split("-")[0];
        String yearSecond = String.valueOf(data.get("report_period_second")).split("-")[0];
        String yearThird = String.valueOf(data.get("report_period_third")).split("-")[0];
        List<ChartLineDataVO> vos = new ArrayList<>();
        int s1 = getSeason(String.valueOf(data.get("report_period_first")));
        int s2 = getSeason(String.valueOf(data.get("report_period_second")));
        int s3 = getSeason(String.valueOf(data.get("report_period_third")));
        vos.add(new ChartLineDataVO("环比增长", yearThird + "Q" + s3, getDoubleData(data, "total_operating_revenue_qoq_growth_rate_third")));
        vos.add(new ChartLineDataVO("环比增长", yearSecond + "Q" + s2, getDoubleData(data, "total_operating_revenue_qoq_growth_rate_second")));
        vos.add(new ChartLineDataVO("环比增长", yearFirst + "Q" + s1, getDoubleData(data, "total_operating_revenue_qoq_growth_rate_first")));

        vos.add(new ChartLineDataVO("同比增长", yearThird + "Q" + s3, getDoubleData(data, "total_operating_revenue_yoy_growth_rate_third")));
        vos.add(new ChartLineDataVO("同比增长", yearSecond + "Q" + s2, getDoubleData(data, "total_operating_revenue_yoy_growth_rate_second")));
        vos.add(new ChartLineDataVO("同比增长", yearFirst + "Q" + s1, getDoubleData(data, "total_operating_revenue_yoy_growth_rate_first")));
        return vos;
    }

    /**
     * 获取股票营收增长率折线图数据(同比和环比)
     *
     * @param stockCode 股票代码
     */
    public List<ChartLineDataVO> queryGrossProfit(String stockCode) {
        Map<String, Object> data = stockFinaceDao.getStockLatestData(stockCode);
        List<ChartLineDataVO> vos = new ArrayList<>();
        String yearFirst = String.valueOf(data.get("report_period_first")).split("-")[0];
        String yearSecond = String.valueOf(data.get("report_period_second")).split("-")[0];
        String yearThird = String.valueOf(data.get("report_period_third")).split("-")[0];
        int s1 = getSeason(String.valueOf(data.get("report_period_first")));
        int s2 = getSeason(String.valueOf(data.get("report_period_second")));
        int s3 = getSeason(String.valueOf(data.get("report_period_third")));

        vos.add(new ChartLineDataVO("环比增长", yearThird + "Q" + s3, getDoubleData(data, "net_profit_margin_qoq_growth_rate_third")));
        vos.add(new ChartLineDataVO("环比增长", yearSecond + "Q" + s2, getDoubleData(data, "net_profit_margin_qoq_growth_rate_second")));
        vos.add(new ChartLineDataVO("环比增长", yearFirst + "Q" + s1, getDoubleData(data, "net_profit_margin_qoq_growth_rate_first")));

        vos.add(new ChartLineDataVO("同比增长", yearThird + "Q" + s3, getDoubleData(data, "net_profit_margin_yoy_growth_rate_third")));
        vos.add(new ChartLineDataVO("同比增长", yearSecond + "Q" + s2, getDoubleData(data, "net_profit_margin_yoy_growth_rate_second")));
        vos.add(new ChartLineDataVO("同比增长", yearFirst + "Q" + s1, getDoubleData(data, "net_profit_margin_yoy_growth_rate_first")));
        return vos;
    }
}
